/* Papastefanos Serafeim */
/* Diafores voh9htikes sunarthseis */

#include <stdio.h>
#include <stdlib.h>
#include "ixml.h"
#include "client.h"
#include "soaparse.h"
#include "soapcrt.h"
#include "helpers.h"
#include "time.h"
#include "string.h"


/* Se auto to array uparxei mia antistoixish metaju twn ari9mwn sfalmatos tou
 * TR69 kai tou mhnumatos pou 9a prepei na emfanistei sthn o9onh. Upoti9etai
 * oti o pinakas jekina apo to 9000. */
static char *errorStrings[] = {
	"Method not supported", //9000
	"Request denied",  //9001
	"Internal error", //9002
	"Invalid arguments", //9003
	"Resources exceeded", //9004
	"Invalid parameter name", //9005
	"Invalid parameter type", //9006
	"Invalid parameter value", //9007
	"Attempt to set a non-writable parameter" //9008
};

/* Emfanizei sthn o9onh to pv */
void viewParameterValueStruct(ParameterValueStruct *pv) {
	PRINTD("Name: %s\n", pv->name);
	PRINTD("Value: %s\n", pv->value);
}

/* Metatrepei mia hmeromhnia se string. Ginetai xrhsh statikhs mnhmhs sthn
 * opoia apo9hkeuetai to string pou 9a epistrafei. Auto ginetai gia na mhn
 * xreiazetai na eleu9erwnetai to memmory buffer pou 9a epestrefe auth h
 * entolh, opws 9a eprepe na ginei an eixame xrhsimopoihsei mnhmh sto heap. 
 * H xrhsh statikhs mnhmhs ektos apo to pleonekthma pou molis shmeiw9hke
 * exei kai ena mikro meionekthma: Den 9a prepei na klh9ei gia deuterh
 * fora h datetoasci an prwta den exei antigrafei se enan allo buffer to
 * apotelesma apo thn prwth klhsh ths. */
char *datetoasci(DateTime *d) {
	static char buf[20];
	sprintf(buf,"%04d-%02d-%02dT%02d:%02d:%02d", d->year, d->month, d->day, d->hour, d->min, d->sec );
	buf[19]=0;
	return buf;
}

/* Metatrepei ena string se DateTime. Opws fainetai to string prepei na
 * exei polu austhrh domh gia na douleyei swsta auth h sunarthsh. */
DateTime *ascitodate(char *buf) {
	DateTime *d = (DateTime *)malloc(sizeof(DateTime) );
	d->year = atoi(buf);
	d->month= atoi(&buf[5]);
	d->day = atoi(&buf[8]);
	d->hour = atoi(&buf[11]);
	d->min = atoi(&buf[14]);
	d->sec = atoi(&buf[17]);
	return d;
}

/* Dhmiourgei mia nea domh ParameterValueList mege9ous size. */
ParameterValueList *newParameterValueList(unsigned int size) {
	ParameterValueList *pl = (ParameterValueList *)malloc(sizeof(ParameterValueList) );
	if(pl==NULL) {
		PRINTD("Memmory errors (ParameterValueList creation)");
		return NULL;
	}
	pl->size=size;
	
	pl->parameters=(ParameterValueStruct *)malloc(size*sizeof(ParameterValueStruct) );
	if(pl->parameters == NULL) {
		PRINTD("Memmory errors (ParameterValueList creation)");
		return NULL;
	}
	unsigned int i;
	for(i=0;i<size;i++) {
		pl->parameters[i].name = NULL; 
		pl->parameters[i].value = NULL; 
		
	}

	return pl;
}

/* Eleu9erwnei thn ParameterValueList pvl */
void freeParameterValueList(ParameterValueList *pvl) {
	unsigned int i;
/*	for(i=0;i<pvl->size;i++) {
		if( pvl->parameters[i].name != NULL )
		free(pvl->parameters[i].name);
		if( pvl->parameters[i].value != NULL )
		free(pvl->parameters[i].value);
		
	}
	if(pvl->size == 0) {
		free(pvl); 	
		return;
	}
	free(pvl->parameters);
	free(pvl);
 */
}

/* Dhmiourgei mia nea EventList mege9ous size */
EventList *newEventList(unsigned int size) {
	EventList *el = (EventList *)malloc(sizeof(EventList) );
	if(el==NULL) {
		PRINTD("Memmory errors (EventList creation)");
		return NULL;
	}
	el->size=size;
	
	el->events=(EventStruct *)malloc(size*sizeof(EventStruct) );
	if(el->events == NULL) {
		PRINTD("Memmory errors (EventList creation)");
		return NULL;
	}
	return el;
}

/* Eleu9erwnei thn EventList evl */
void freeEventList(EventList *evl) {
	unsigned int i;
	for(i=0;i<evl->size;i++) {
		free(evl->events[i].eventCode);
	}
	if(evl->size == 0) {
		free(evl); 	
		return;
	}
	free(evl->events);
	free(evl); 
}

/* Dhmiourgei mia nea StringList mege9ous size */
StringList *newStringList(unsigned int size) {
	StringList *sl = (StringList *)malloc(sizeof(StringList) );
	if(sl==NULL) {
		PRINTD("Memmory errors (StringList creation)");
		return NULL;
	}
	sl->size=size;
	
	sl->strings=(char **)malloc(size*sizeof(char *) );
	if(sl->strings == NULL) {
		PRINTD("Memmory errors (StringList creation)");
		return NULL;
	}
	return sl;
}

/* Eleu9erwnei th StringList stl */
void freeStringList(StringList *stl ) {
	unsigned int i;
	for(i=0;i<stl->size;i++) {
		free(stl->strings[i]);
	}
	if(stl->size == 0) {
		free(stl); 	
		return;
	}
	free(stl->strings);
	free(stl); 
}

/* H sunarthsh auth epistrefei mia domh DateTime me thn wra tou upologisth.
 * Xrhsimopoiei thn ansi sunarthsh ltime h opoia epistrefei mia domh tm. */
DateTime *getCurrentTime(void) {
	DateTime *currentTime = (DateTime *)malloc(sizeof(DateTime) );
	if(currentTime == NULL) return NULL;
	time_t t = time(NULL);
	struct tm *ltime = localtime(&t);
	if(ltime == NULL) return NULL;
	currentTime->day=ltime->tm_mday;
	currentTime->hour=ltime->tm_hour;
	currentTime->min=ltime->tm_min;
	currentTime->month=ltime->tm_mon;
	currentTime->sec=ltime->tm_sec;
	currentTime->year=ltime->tm_year+1900;
	return currentTime;
}

/* Epistrefei mia perigrafh tou sfalmatos, analoga me ton ari9mo tou. Ginetai
 * xrhsh tou pinaka errorStrings pou oristhke pio panw. */
char *getErrorByNum(int num) {
	num-=9000;
	if( (num < 0)||(num > 8) ) return NULL;
	else return errorStrings[num];


}


#if defined(_NO_DEBUG) && defined(__GNUC__)
	/* Nothing */
#else
	void PRINTD(char* format, ...) {
	#ifdef NDEBUG
		/* Empty body */
	#else
	        va_list args;
	
	        va_start(args, format);
	        vfprintf(stderr, format, args);
	        va_end(args);
	#endif 
	}
#endif
